package com.dayouzi.crawler_monitor.controller;

import com.dayouzi.crawler_monitor.base.result.R;
import com.dayouzi.crawler_monitor.pojo.page.PageBean;
import com.dayouzi.crawler_monitor.pojo.vo.CrawlerDataVO;
import com.dayouzi.crawler_monitor.pojo.vo.CrawlerDetailVO;
import com.dayouzi.crawler_monitor.pojo.vo.YejiDetailVO;
import com.dayouzi.crawler_monitor.service.YejiDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;


@Api(description = "业绩爬虫详情")
@RestController
@RequestMapping("/crawler/web")
@Slf4j
public class YejiDetailController {

    @Autowired
    private YejiDetailService yejiDetailService;

    @ApiOperation("获取数据信息")
    @GetMapping("/yejiDetail")
    public R yejiDetail(
            @ApiParam(value = "id", required = true) @RequestParam("id") Integer id
    ) {
        YejiDetailVO result = yejiDetailService.yejiDetail(id);
        return R.ok().data("result", result);
    }

    @ApiOperation("获取业绩爬虫list")
    @GetMapping("/yejiList")
    public R yejiList(
            @ApiParam(value = "tableName", required = true) @RequestParam("tableName") String tableName
    ) {
        String[] splits = tableName.split(">");
        String machine = splits[0];
        ArrayList<String> fieldList = new ArrayList<>();
        if (machine.equals("241")) {
            fieldList = yejiDetailService.getYejiFieldList241(tableName);
        } else if (machine.equals("252")) {
            fieldList = yejiDetailService.getYejiFieldList252(tableName);
        }

        return R.ok().data("fieldList", fieldList);
    }

    /**
     * 分页查询爬虫信息list
     * @param page
     * @param limit
     * @param tableName
     * @return
     */
    @ApiOperation("分页查询爬虫信息list")
    @PostMapping("/yejiDataList")
    public R listPage(
            @ApiParam(value = "当前页码", required = true) @RequestParam Integer page,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer limit,
            @ApiParam(value = "查询对象", required = true) @RequestParam("tableName") String tableName
    ) {
        PageBean<HashMap<String, String>> list = null;
        String[] splits = tableName.split(">");
        String machine = splits[0];
        if (machine.equals("241")) {
            list = yejiDetailService.getYejiDataList241(page, limit, tableName);
        } else if (machine.equals("252")) {
            list = yejiDetailService.getYejiDataList252(page, limit, tableName);
        }
        return R.ok().data("list", list);
    }


}
